<?php
// 引入数据库连接文件
if (!isset($con)) {
    die("Database connection variable is not set.");
}
?>
<div id="middle">
    <div id="left">
        <div id="up">
            <div class="news-title">热点新闻</div>
            <div id="news">
                <ul>
                    <?php
                    $newssql = "select * from news order by id desc limit 0,5";
                    $newsquery = mysqli_query($con, $newssql);
                    // 移除多余的 mysqli_fetch_array 调用
                    while ($newsrow = mysqli_fetch_array($newsquery, MYSQLI_ASSOC)) {
                    ?>
                        <li class="news"><a href="newslist.php?newsid=<?php echo $newsrow['id']; ?>"><?php echo substr($newsrow['title'], 0, 80); ?></a></li>
                    <?php
                    }
                    ?>
                </ul>
                <div class="more-link"><a href="newslist.php">更多新闻 >></a></div>
            </div>
        </div>
        <div id="down">
            <div class="news-title">个人信息</div>
            <ul class="profile-list">
                <?php
                $sql = "select * from qiye";
                $query = mysqli_query($con, $sql);
                $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
                ?>
                <ul style="list-style-type: none; padding: 0;">
                    <li style="margin:2px;">电话：<?php echo $row['tel'] ?></li>
                    <li style="margin:2px;">传真：<?php echo $row['chuanzhen'] ?></li>
                    <li style="margin:2px;">邮箱：<?php echo $row['email'] ?></li>
                    <li style="margin:2px;">业务部负责人：<?php echo $row['yewuname'] ?></li>
                    <li style="margin:2px;">技术部负责人：<?php echo $row['jishuname'] ?></li>
                    <li style="margin:2px;">技术咨询电话：<?php echo $row['jishutel'] ?></li>

                </ul>
            </ul>
            <img src="images/wx1.png" class="wechat-qr" />
        </div>
    </div>
    <div id="right">
        <div class="pro">
            <div class="news-title">用户留言</div>
            <div id="news-con">
                <?php
                // Check if the form is submitted
                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                    $name = $_POST['lyname'];
                    $title = $_POST['lytitle'];
                    $email = $_POST['lyemail'];
                    $content = $_POST['lycontent'];
                    
                    // Insert data into the database
                    $sql = "INSERT INTO liuyan (name, title, email, content, time) VALUES ('$name', '$title', '$email', '$content', now())";
                    mysqli_query($con, $sql);
                }

                // 当前页码（从GET参数获取，默认为1）
                $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                // 每页显示数量
                $perPage = 4;

                // 获取总记录数
                $countSql = "SELECT COUNT(*) AS total FROM liuyan";
                $countQuery = mysqli_query($con, $countSql);
                $countRow = mysqli_fetch_assoc($countQuery);
                $totalRows = $countRow['total'];
                // 计算总页数
                $totalPages = ceil($totalRows / $perPage);
                // 处理当前页边界
                $page = max(1, min($page, $totalPages)); // 确保页码在1到总页数之间
                // 计算偏移量
                $offset = ($page - 1) * $perPage;
                ?>

                <?php
                if(isset($_GET['lyid'])) {
                    if(isset($_GET['del']) && $_GET['del'] == "yes") {
                        $delsql = "delete from liuyan where id='" . mysqli_real_escape_string($con, $_GET['lyid']) . "'"; 
                        mysqli_query($con, $delsql);
                        echo "<script>window.location.href='book.php'</script>";
                    }
                }
                ?>

                <?php
                // 如果有详情请求，显示详细信息
                if(isset($_GET['lyid']) && $_GET['action']=="detail") {
                    $detailsql = "SELECT * FROM liuyan WHERE id='".$_GET['lyid']."'";
                    $detailquery = mysqli_query($con, $detailsql);
                    $detail = mysqli_fetch_array($detailquery, MYSQLI_ASSOC);
                    if($detail) {
                        echo '<div class="message-detail">';
                        echo '<h3>留言详情</h3>';
                        echo '<p><strong>姓名：</strong>'.$detail['name'].'</p>';
                        echo '<p><strong>邮箱：</strong>'.$detail['email'].'</p>';
                        echo '<p><strong>标题：</strong>'.$detail['title'].'</p>';
                        echo '<p><strong>内容：</strong>'.$detail['content'].'</p>';
                        echo '<p><strong>时间：</strong>'.$detail['time'].'</p>';
                        echo '<p><a href="book.php">返回留言列表</a></p>';
                        echo '</div>';
                    }
                } else {
                    // 显示留言列表和表单
                    echo '<form name="lyform" method="POST" action="" onsubmit="return Checkly();">';
                    echo '<label for="lyname">姓名：</label>';
                    echo '<input type="text" name="lyname"><br>';
                    echo '<label for="lyemail">邮箱：</label>';
                    echo '<input type="text" name="lyemail"><br>';
                    echo '<label for="lytitle">标题：</label>';
                    echo '<input type="text" name="lytitle"><br>';
                    echo '<label for="lycontent">留言：</label><br>';
                    echo '<textarea name="lycontent" placeholder="在这里输入您的留言"></textarea><br>';
                    echo '<input type="submit" value="提交留言">';
                    echo '</form>';
                    
                    echo '<table class="message-table">';
                    echo '<tr>';
                    echo '<th>姓名</th>';
                    echo '<th>标题</th>';
                    echo '<th>内容</th>';
                    echo '<th>操作</th>';
                    echo '</tr>';

                    // 查询当前页的数据
                    $lysql = "SELECT * FROM liuyan ORDER BY id DESC LIMIT $offset, $perPage";
                    $lyquery = mysqli_query($con, $lysql);
                    while($row=mysqli_fetch_array($lyquery,MYSQLI_ASSOC)) {
                        echo '<tr>';
                        echo '<td>'.htmlspecialchars($row['name']).'</td>';
                        echo '<td>'.htmlspecialchars($row['title']).'</td>';
                        echo '<td>'.htmlspecialchars($row['content']).'</td>';
                        echo '<td><a href="book.php?lyid='.$row['id'].'&action=detail">查看详情</a></td>';
                        echo '</tr>';
                    }
                    echo '</table>'
                    ?>
                </table>

                <?php
                if ($totalPages > 1) {
                    echo '<div class="pagination">';
                    // 首页
                    if ($page > 1) {
                        echo '<a href="book.php?page=1">首页</a>';
                    }
                    // 上一页
                    if ($page > 1) {
                        echo '<a href="book.php?page='.($page-1).'">&laquo; 上一页</a>';
                    }
                    
                    // 页码
                    for ($i = 1; $i <= $totalPages; $i++) {
                        if ($i == $page) {
                            echo '<a class="current" href="book.php?page='.$i.'">'.$i.'</a>';
                        } else {
                            echo '<a href="book.php?page='.$i.'">'.$i.'</a>';
                        }
                    }
                    
                    // 下一页
                    if ($page < $totalPages) {
                        echo '<a href="book.php?page='.($page+1).'">下一页 &raquo;</a>';
                    }
                    // 尾页
                    if ($page < $totalPages) {
                        echo '<a href="book.php?page='.$totalPages.'">尾页</a>';
                    }
                    echo '</div>';
                }
                ?>
            </div>
            </div>
        </div>
    </div>
                <?php } // 结束留言列表的else语句块 ?>
            </div>
        </div>
    </div>
</div>